package com.sansecy.customview.device;

import android.util.Log;

import java.lang.reflect.Method;

public class GetKernel {
    private static final String TAG = "MySystemProperties";

    public static String get(String key) {
        init();
        String value = null;
        try {
            value = (String) mGetMethod.invoke(mClassType, key);
        } catch (Exception e) {
            e.printStackTrace();
        }
        for (Method method : methods){
            Log.i("*******Method********",method.toString());
        }
        return value;
    }
    public static String set(String key) {
        init();
        String value = null;
        try {
            value = (String) mSetMethod.invoke(mClassType, key);
        } catch (Exception e) {
            e.printStackTrace();
        }

        for (Method method : methods){
            Log.i("*******Method********",method.toString());
        }
        return value;
    }

    //int SystemProperties.get(String key, int def){}

    public static int getInt(String key, int def) {
        init();

        int value = def;

        try {

            value = (Integer) mGetIntMethod.invoke(mClassType, key, def);

        } catch (Exception e) {

            e.printStackTrace();

        }

        return value;

    }

    public static int getSdkVersion() {

        return getInt("ro.build.version.sdk", -1);

    }

    private static Class<?> mClassType = null;

    private static Method mGetMethod = null;
    private static Method mSetMethod = null;

    private static Method mGetIntMethod = null;
    private static Method [] methods;

    private static void init() {
        try {
            if (mClassType == null) {

                mClassType = Class.forName("android.os.SystemProperties"); //通过Class.forname获取该Class对象;

                mGetMethod = mClassType.getDeclaredMethod("get", String.class);
                mSetMethod = mClassType.getDeclaredMethod("set", String.class);

                mGetIntMethod = mClassType.getDeclaredMethod("getInt", String.class, int.class);

                methods = mClassType.getMethods();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}